package com.nlp.server.service;

import com.nlp.common.core.constant.UserConstants;
import com.nlp.common.core.exception.ServiceException;

import com.nlp.common.core.result.Result;
import com.nlp.common.core.utils.StringUtils;
import com.nlp.common.core.utils.ip.IpUtils;
import com.nlp.server.controller.SysUserController;
import com.nlp.server.pojo.dto.LoginDTO;
import com.nlp.server.pojo.entity.LoginUser;
import com.nlp.server.pojo.entity.SysUser;
import com.nlp.server.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * 登录校验方法
 * 
 * @author shiliuyinzhen
 */
@Component
public class LoginService {

    @Autowired
    private SysUserController userController;

    @Autowired
    private PasswordService passwordService;

    /**
     * 用户登录
     * @param loginDTO
     * @return
     */
    public LoginUser login(LoginDTO loginDTO) {

        //获取用户名及密码
        String userName =  loginDTO.getUserName();
        String password = loginDTO.getPassword();

        // 用户名或密码为空 错误
        if (StringUtils.isAnyBlank(userName, password)) {
            throw new ServiceException("用户/密码必须填写");
        }
        // 密码如果不在指定范围内 错误
        if (password.length() < UserConstants.PASSWORD_MIN_LENGTH
                || password.length() > UserConstants.PASSWORD_MAX_LENGTH) {
            throw new ServiceException("用户密码长度不在指定范围");
        }
        // 用户名不在指定范围内 错误
        if (userName.length() < UserConstants.USERNAME_MIN_LENGTH
                || userName.length() > UserConstants.USERNAME_MAX_LENGTH) {
            throw new ServiceException("用户名长度不在指定范围");
        }
        // 查询用户信息
        Result<LoginUser> userResult = userController.info(userName);
        if (StringUtils.isNull(userResult) || StringUtils.isNull(userResult.getData())) {
            throw new ServiceException("登录用户：" + userName + " 不存在");
        }
        if (Result.FAIL == userResult.getCode()) {
            throw new ServiceException(userResult.getMsg());
        }
        LoginUser userInfo = userResult.getData();
        SysUser sysUser = userResult.getData().getSysUser();
        userName = sysUser.getUserName();

        passwordService.validate(sysUser, password);

        //更新用户登录时间和ip
        sysUser.setLoginIp(IpUtils.getIp());
        sysUser.setLoginTime(LocalDateTime.now());
        userController.updateLoginInfo(sysUser);
        return userInfo;
    }

    /**
     * 用户注册
     * @param sysUser
     * @return
     */
    public void register(SysUser sysUser) {

        String userName =  sysUser.getUserName();
        String nickName =  sysUser.getNickName();
        String password =  sysUser.getPassword();

        // 用户名或密码为空 错误
        if (StringUtils.isAnyBlank(userName, password)) {
            throw new ServiceException("用户/密码必须填写");
        }
        if (StringUtils.isAnyBlank(nickName, password)) {
            throw new ServiceException("用户昵称必须填写");
        }
        if (userName.length() < UserConstants.USERNAME_MIN_LENGTH
                || userName.length() > UserConstants.USERNAME_MAX_LENGTH) {
            throw new ServiceException("用户名长度必须在2到20个字符之间");
        }
        if (nickName.length() < UserConstants.USERNAME_MIN_LENGTH
                || nickName.length() > UserConstants.USERNAME_MAX_LENGTH) {
            throw new ServiceException("昵称长度必须在2到20个字符之间");
        }
        if (password.length() < UserConstants.PASSWORD_MIN_LENGTH
                || password.length() > UserConstants.PASSWORD_MAX_LENGTH) {
            throw new ServiceException("密码长度必须在5到20个字符之间");
        }

        // 注册用户信息
        sysUser.setUserName(userName);
        sysUser.setNickName(nickName);
        sysUser.setPassword(SecurityUtils.encryptPassword(password));
        Result<?> registerResult = userController.register(sysUser);

        if (Result.FAIL == registerResult.getCode()) {
            throw new ServiceException(registerResult.getMsg());
        }
    }
}
